class Compiler {
    constructor(vm) {
        this.vm = vm;
        this.el = vm.$el;
        this.compile(this.el);
    }
    // 编译模版 处理文本节点和元素节点
    compile(el) {
        let childNodes = el.childNodes;
        Array.from(childNodes).forEach(node => {
            if (this.isTextNode(node)) {
                this.compileText(node);
            } else if (this.isElementNode(node)) {
                this.compileElement(node);
            }
            if (node.childNodes && node.childNodes.length) {
                this.compile(node);
            }
        });
    }
    // 编译元素节点，处理指令
    compileElement(node) {
        Array.from(node.attributes).forEach(attr => {
            let attrName = attr.name
            if (this.isDirective(attrName)) {
                attrName = attrName.substr(2)
                let key = attr.value
                this.update(node, key, attrName)
            }
        })
    }
    update(node, key, attrName) {
        let updateFn = this[attrName + 'Updater']
        updateFn && updateFn.call(this, node, this.vm[key], key)
    }
    textUpdater(node, val, key) {
        node.textContent = val
        new Watcher(this.vm, key, newValue => {
            node.textContent = newValue
        })
    }
    modelUpdater(node, val, key) {
        node.value = val
        new Watcher(this.vm, key, newValue => {
            node.value = newValue
        })

        node.addEventListener('input', () => {
            this.vm[key] = node.value
        })
    }
    // 编译文本节点，处理差值表达式 {{ mag }}
    compileText(node) {
        let reg = /\{\{(.+?)\}\}/g;
        let value = node.textContent;
        if (reg.test(value)) {
            let keys = value.match(reg);
            keys.forEach(sub => {
                let subReg = /\{\{(.+?)\}\}/;
                subReg.test(sub);
                let key = RegExp.$1.trim();
                try {
                    let val = eval('this.vm.' + key)
                    node.textContent = node.textContent.replace(sub, val);
                    new Watcher(this.vm, key, newValue => {
                        node.textContent = newValue
                    })
                } catch (error) {
                    throw new Error('模板解析出错了.' +  error)
                }
            });
        }
    }
    // 判断元素属性是否是指令
    isDirective(attrName) {
        return attrName.startsWith("v-");
    }
    // 判断节点是否为文本节点
    isTextNode(node) {
        return node.nodeType === 3;
    }
    // 判断节点是否为元素节点
    isElementNode(node) {
        return node.nodeType === 1;
    }
}
